package com.web.surf.sample.eshop.faces.backing.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import com.web.surf.sample.eshop.database.manager.interfaces.IGoodsService;
import com.web.surf.sample.eshop.faces.managed.beans.GoodsBean;
import com.web.surf.sample.eshop.faces.managed.beans.OrderBean;

public class BasketBacking implements Serializable {

	private static final long serialVersionUID = -7644773529357861000L;
	private int chosenGoodsNumber = 1;
	private IGoodsService goodsService;
	private String chooseParam;
	private GoodsBean current;
	private OrderBean orderBean;

	public void choose(ActionEvent ae) {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> params = context.getExternalContext()
				.getRequestParameterMap();
		chooseParam = params.get("choose");
	}

	public void confirm(ActionEvent ae) {
		current = goodsService.getGoodsBean(chooseParam);
		addGoods();
		calculateGoodsNumber();
		calculateTotalPrice();
		chosenGoodsNumber = 1;
		chooseParam = null;
	}

	public void reject(ActionEvent ae) {
		chosenGoodsNumber = 1;
		chooseParam = null;
	}

	public void remove(ActionEvent ae) {
		choose(ae);
		GoodsBean toDelete = null;
		for (GoodsBean s : orderBean.getBasket().keySet()) {
			if (chooseParam.equals(s.getGoodsID())) {
				toDelete = s;
			}
		}
		orderBean.getBasket().remove(toDelete);
		calculateGoodsNumber();
		calculateTotalPrice();
		chooseParam = null;
	}

	private void addGoods() {
		boolean have = false;
		for (GoodsBean s : orderBean.getBasket().keySet()) {
			if (s.getGoodsID() == current.getGoodsID()) {
				have = true;
				int number = orderBean.getBasket().get(s);
				orderBean.getBasket().put(s, number + chosenGoodsNumber);
			}
		}
		if (!have) {
			orderBean.getBasket().put(current, chosenGoodsNumber);
		}
	}

	public void clearAll(ActionEvent ae) {
		orderBean.getBasket().clear();
		orderBean.setTotalPrice(0);
		orderBean.setTotalGoodsNumber(0);
	}

	private void calculateGoodsNumber() {
		Collection<Integer> goods = orderBean.getBasket().values();
		Iterator<Integer> i = goods.iterator();
		orderBean.setTotalGoodsNumber(0);
		while (i.hasNext()) {
			int current = i.next();
			int totalGoodsNumber = orderBean.getTotalGoodsNumber();
			orderBean.setTotalGoodsNumber(totalGoodsNumber + current);
		}
	}

	private void calculateTotalPrice() {
		orderBean.setTotalPrice(0);
		for (GoodsBean s : orderBean.getBasket().keySet()) {
			double totalPrice = orderBean.getTotalPrice();
			orderBean.setTotalPrice(totalPrice + s.getPriceBYRValue()
					* orderBean.getBasket().get(s));
		}
	}

	public List<GoodsBean> getGoodsBeans() {
		List<GoodsBean> goodsBeans = new ArrayList<GoodsBean>();
		for (GoodsBean s : orderBean.getBasket().keySet()) {
			goodsBeans.add(s);
		}
		return goodsBeans;
	}

	public int getChosenGoodsNumber() {
		return chosenGoodsNumber;
	}

	public String getChooseParam() {
		return chooseParam;
	}

	public void setChosenGoodsNumber(int chosenGoodsNumber) {
		this.chosenGoodsNumber = chosenGoodsNumber;
	}

	// Spring setters
	public void setGoodsService(IGoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public void setOrderBean(OrderBean orderBean) {
		this.orderBean = orderBean;
	}
}